package cn.zyjblogs.sms.provider;


import cn.zyjblogs.sms.common.SmsRequest;
import cn.zyjblogs.sms.common.SmsResponse;
import cn.zyjblogs.sms.task.constant.ProviderType;
import cn.zyjblogs.sms.task.constant.SendStatus;
import cn.zyjblogs.sms.task.constant.TaskStatus;
import cn.zyjblogs.sms.task.dto.TaskResultDTO;
import cn.zyjblogs.starter.common.entity.constant.MobileRegularExp;
import cn.zyjblogs.starter.common.utils.collect.Lists;
import cn.zyjblogs.starter.common.utils.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author zhuyijun
 */
public abstract class AbstractSmsGranter implements SmsGranter {
    private static final Logger log = LoggerFactory.getLogger(AbstractSmsGranter.class);

    protected AbstractSmsGranter(ProviderType providerType) {
    }

    @Override
    public SmsGranter getGranter(ProviderType providerType) {
        return null;
    }

    @Override
    public TaskResultDTO sendSms(ProviderType providerType, SmsRequest smsRequest) {
        if (CollectionUtils.isEmpty(smsRequest.getTelNums())) {
            return TaskResultDTO.builder().status(TaskStatus.FAIL).build();
        }
        List<String> telNums = smsRequest.getTelNums();
        //不发送手机号（格式不对）
        List<String> noPhones = new ArrayList<>();
        //准备发送短信的手机号
        List<String> sendPhones = new ArrayList<>();
        telNums.forEach(phone -> {
            if (!MobileRegularExp.isMobileNumber(phone)) {
                noPhones.add(phone);
            } else {
                sendPhones.add(phone);
            }
        });
        List<List<String>> phones = Lists.partition(sendPhones, providerType.getMaxNum());
        List<SmsResponse> smsResponses = new LinkedList<>();
        if (!CollectionUtils.isEmpty(noPhones)) {
            smsResponses.addAll(assembleSendSmsResponse(noPhones, "手机号格式不对，未进行发送"));
        }
        log.info("任务id: {} 正在发送短信", smsRequest.getTaskId());
        phones.forEach(phone -> {
            smsRequest.setTelNums(phone);
            List<SmsResponse> list = sendSms(smsRequest);
            if (!CollectionUtils.isEmpty(list)) {
                smsResponses.addAll(list);
            } else {
                smsResponses.addAll(assembleSendSmsResponse(phone, "发送失败"));
            }
        });
        log.info("任务id: {} 短信发送完成", smsRequest.getTaskId());
        return TaskResultDTO.builder().responses(smsResponses).status(TaskStatus.SUCCESS).build();
    }

    private List<SmsResponse> assembleSendSmsResponse(List<String> phone, String content) {
        List<SmsResponse> smsResponses = new LinkedList<>();
        phone.forEach(tel -> smsResponses.add(SmsResponse.builder()
                .phoneNumber(tel)
                .sendStatus(SendStatus.FAIL)
                .message(content)
                .code("-1")
                .build()));
        return smsResponses;
    }

    /**
     * 批量短信发送
     *
     * @param smsRequest
     * @return
     */
    public abstract List<SmsResponse> sendSms(SmsRequest smsRequest);
}
